package com.gs.SynAntiWandering.fragment;

import android.content.Intent;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.activity.member.MemberEditActivity;
import com.gs.SynAntiWandering.adapter.MemberAdapter;
import com.gs.SynAntiWandering.apiresult.ApiResultData;
import com.gs.SynAntiWandering.apiresult.PagedList;
import com.gs.SynAntiWandering.constant.AllConstants;
import com.gs.SynAntiWandering.constant.EventCode;
import com.gs.SynAntiWandering.control.LinearLayoutManager;
import com.gs.SynAntiWandering.db.bean.Event;
import com.gs.SynAntiWandering.db.dto.MemberDto;
import com.gs.SynAntiWandering.db.dto.ShortDto;
import com.gs.SynAntiWandering.db.query.MemberQuery;
import com.gs.SynAntiWandering.dialog.CommDialogUtils;
import com.gs.SynAntiWandering.dialog.LoadingDialogUtils;
import com.gs.SynAntiWandering.fragment.base.BaseOptionListFragment;
import com.gs.SynAntiWandering.presenter.MemberPresenter;
import com.gs.SynAntiWandering.util.SPUtil;
import com.gs.SynAntiWandering.util.ToastUtil;
import com.gs.SynAntiWandering.view.IMemberView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.List;

public class MemberFragment extends BaseOptionListFragment<IMemberView, MemberPresenter,
        MemberDto, MemberQuery> implements IMemberView {

    private MemberAdapter adapter;
    private int typeKey;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_member;
    }

    @Override
    public MemberPresenter createPresenter() {
        return new MemberPresenter();
    }

    @Override
    public MemberQuery createQuery() {
        return new MemberQuery();
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        adapter = new MemberAdapter(getActivity(), context, mData, R.layout.rvitem_member);
        adapter.setOnItemClickListener((v, position) -> {
            if (mData.isEmpty()) {
                ToastUtil.showToast(R.string.refresh_data);
                return;
            }
            update(position);
        });
        adapter.setOnItemLongClickListener((v, position) -> {
            if (mData.isEmpty()) {
                ToastUtil.showToast(R.string.refresh_data);
                return;
            }
            CommDialogUtils.showDialog(context, "", getString(R.string.delete_warning), new CommDialogUtils.DialogClickListener() {
                @Override
                public void cancelClick() {
                }

                @Override
                public void okClick() {
                    delete(position);
                }
            });
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void delete(int position) {
        try {
            mPresenter.delete(mData.get(position).getId(), position);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(int position) {
        Intent intent = new Intent();
        intent.setClass(context, MemberEditActivity.class);
        intent.putExtra(AllConstants.ID, mData.get(position).getId());
        startActivityForResult(intent, 1);
    }

    @Override
    public void loadOptionData() {
        typeKey = SPUtil.getInstance().getInt(AllConstants.INFO_TYPE, 1);
        mPresenter.combo(typeKey == 1 ? "floors" : "memberTypes");
    }

    @Override
    public void add() {
        Intent intent = new Intent();
        intent.setClass(context, MemberEditActivity.class);
        startActivityForResult(intent, 1);
    }

    @Override
    public void search() {
        mData.clear();
        query.init();
        query.setKeyWord(toolBarSearch.getText());
        mPresenter.getData(query);
    }

    @Override
    public void optionItem(Object option) {
        if (option == null) return;
        ShortDto dto = (ShortDto) option;
        String id = dto.getId();
        query.setFloorId(null);
        query.setMemberTypeId(null);
        if (!TextUtils.isEmpty(id)) {
            if (typeKey == 1) {
                query.setFloorId(id);
            } else {
                query.setMemberTypeId(id);
            }
        }
        search();
    }

    @Override
    public void loadData() {
        mPresenter.getData(query);
    }

    @Override
    public void onGetDataSuccess(ApiResultData<PagedList<MemberDto>> apiResult) {
        refreshSmartRefreshLayout();
        if (apiResult.getStatus() != 2000) {
            ToastUtil.showToast(apiResult.getMessage());
            adapter.notifyDataSetChanged();
            return;
        }
        mData.addAll(apiResult.getData().getList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onGetDataFails(Exception e) {
        refreshSmartRefreshLayout();
        adapter.notifyDataSetChanged();
        ToastUtil.showToast( R.string.network_error);
    }

    @Override
    public void onGetComboSuccess(ApiResultData<List<ShortDto>> apiResult) {
        if (apiResult.getStatus() != 2000) {
            return;
        }
        toolBarOption.setList(apiResult.getData());
    }

    @Override
    public void onGetComboFails(Exception msg) {
        ToastUtil.showToast( R.string.network_error);
    }

    @Override
    public void onDeleteMemberSuccess(ApiResultData<String> apiResultData, int position) {
        ToastUtil.showToast(apiResultData.getMessage());
        if (apiResultData.getStatus() == 2000) {
            adapter.delete(position);
        }
    }

    @Override
    public void onDeleteMemberFails(Exception msg) {
        ToastUtil.showToast( R.string.network_error);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        if (event.getWhat() == EventCode.EVENT_MEMBER_TYPE) {
            loadOptionData();
            search();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1) {
            search();
        }
    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(context);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }
}
